package com.harry.greendaomaster.view;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.harry.greendaomaster.MyApp;
import com.harry.greendaomaster.R;
import com.harry.greendaomaster.adapter.UserAdapter;
import com.harry.greendaomaster.base.BaseActivity;
import com.harry.greendaomaster.db.User;
import com.harry.greendaomaster.greendao.gen.DaoMaster;
import com.harry.greendaomaster.greendao.gen.DaoSession;
import com.harry.greendaomaster.greendao.gen.UserDao;

import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private UserDao userDao;
    private EditText mName;
    private EditText mPwd;
    private EditText mMsg;
    private Button mAdd;
    private Button mDelete;
    private Button mUpdate;
    private Button mSelect;
    private ImageView mImg;
    private RecyclerView mRecycle;

    @Override
    protected void initView() {
        mName = findViewById(R.id.name);
        mPwd = findViewById(R.id.pwd);
        mMsg = findViewById(R.id.msg);
        mAdd = findViewById(R.id.add);
        mDelete = findViewById(R.id.delete);
        mUpdate = findViewById(R.id.update);
        mSelect = findViewById(R.id.select);
        mImg = findViewById(R.id.img);
        mRecycle = findViewById(R.id.recycle);
        mAdd.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mUpdate.setOnClickListener(this);
        mSelect.setOnClickListener(this);
        mImg.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApp.getContext(), "harry-db", null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        userDao = daoSession.getUserDao();
    }

    @Override
    protected int createViews() {
        return R.layout.activity_main;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                insertUser();
                break;
            case R.id.delete:
                deleteUser();
                break;
            case R.id.update:
                updateUser();
                break;
            case R.id.select:
                selectUser();
                break;
        }
    }

    /**
     * 添加
     */
    private void insertUser() {
        String userName = mName.getText().toString().trim();
        String passWord = mPwd.getText().toString().trim();
        Toast.makeText(this,userName + "---" + passWord, Toast.LENGTH_SHORT).show();

        User user = new User(null, userName, passWord);
        userDao.insert(user);
    }

    /**
     * 删除
     */
    private void deleteUser() {
        String name = mName.getText().toString();

//                userDao.deleteByKey ();
        userDao.queryBuilder().where(UserDao.Properties.Username.eq(name)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 修改
     */
    private void updateUser() {
        String name = mName.getText().toString();
        String msg = mMsg.getText().toString();

        /**
         * 修改多条重复数据
         */
        List<User> list = userDao.queryBuilder().where(UserDao.Properties.Username.eq(name)).build().list();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String username = list.get(i).getUsername();
                list.get(i).setUsername(msg);
                userDao.update(list.get(i));
            }
        }

        /**
         * 修改单条数据
         */
//        User user = userDao.queryBuilder().where(UserDao.Properties.Username.eq(name)).build().unique();
//        Toast.makeText(MainActivity.this, "" + name, Toast.LENGTH_SHORT).show();
//        if (user != null) {
//            user.setUsername(msg);
//
//            userDao.update(user);
//        }
    }

    /**
     * 查询
     */
    private void selectUser() {
        List<User> list = userDao.queryBuilder().list();

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycle.setLayoutManager(manager);
        UserAdapter userAdapter = new UserAdapter(this, list);
        mRecycle.setAdapter(userAdapter);
    }
}
